import sys
from datetime import datetime

from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QFrame, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QComboBox, QApplication


class LabelBase(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedHeight(40)
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(0, 0, 0, 0)
        self.hBox.setSpacing(15)
        self.label = QLabel()
        self.label.setTextInteractionFlags(Qt.TextSelectableByMouse)
        self.hBox.addWidget(self.label)

    def setLabelText(self, text):
        self.label.setText(text)

    def labelText(self):
        return self.label.text()


class FileTextLabel(LabelBase):
    clicked = pyqtSignal(str)

    def __init__(self, parent=None, *, _type='openDir'):
        super(FileTextLabel, self).__init__(parent=parent)
        self.label.setFixedWidth(150)
        self.lineEdit = QLineEdit()
        self.pushButton = QPushButton('打开文件')
        self.pushButton.clicked.connect(lambda: self._pushButtonClickedEvent(_type))
        self.pushButton.setFixedWidth(150)
        self.hBox.addWidget(self.lineEdit)
        self.hBox.addWidget(self.pushButton)

    def openDir(self):
        file = QFileDialog.getExistingDirectory(self, '选择文件夹', './')
        return file

    def saveFile(self):
        file, _type = QFileDialog.getSaveFileName(self, '保存shp文件',
                                                  './{}.shp'.format(datetime.now().strftime('%Y%m%d%H%M%S')),
                                                  'shp(*.shp);;')
        return file

    def openFile(self):
        file, _type = QFileDialog.getOpenFileName(self, '打开模板文件', './', 'xlsx(*.xlsx);;')
        return file

    def _pushButtonClickedEvent(self, t):
        """
        按钮事件
        :param t: openDir, saveFile
        :return:
        """
        file = getattr(self, t)()
        if file == '':
            return
        self.lineEdit.setText(file)
        self.clicked.emit(file)

    def setLineEditText(self, text):
        self.lineEdit.setText(text)

    def lineEditText(self):
        return self.lineEdit.text()


class ComboBoxLabel(LabelBase):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.comboBox = QComboBox()
        self.hBox.addWidget(self.comboBox)

    def addItem(self, *args):
        """
        addItem(self, text: Optional[str], userData: Any = None)
        addItem(self, icon: QIcon, text: Optional[str], userData: Any = None)
        :return:
        """
        self.comboBox.addItem(*args)

    def addItems(self, *args, **kwargs):
        """
        addItems(self, texts: Iterable[Optional[str]])
        """
        self.comboBox.addItems(*args, **kwargs)

    def setCurrentIndex(self, index: int):
        self.comboBox.setCurrentIndex(index)

    def currentText(self):
        return self.comboBox.currentText()

    def currentIndex(self):
        self.comboBox.currentIndex()

    def clear(self):
        self.comboBox.clear()


if __name__ == '__main__':
    QApplication.setHighDpiScaleFactorRoundingPolicy(
        Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
    )
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
    app = QApplication(sys.argv)
    win = ComboBoxLabel()
    win.show()
    sys.exit(app.exec_())
